﻿function GetLocationURL(index) {
    var location = window.location.pathname.split('/');
    return location[index];
}

function GetURLParameter(sParam) {
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }

    return '';
}

function GetStringURLParameter(URL, sParam) {
    var sPageURL = URL.split("?");
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }

    return '';
}

function ValidateNumber(e) {
    var evt = (e) ? e : window.event;
    var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

$(document).ready(function () {
    var URL = window.location.href + "/AjaxTable";
    var param = [];
    var page = 1;
    var sort = "";
    var direction = "";

    var menuActive = (GetLocationURL(1)).toLowerCase();
    if (!menuActive) menuActive = "home";
    $("#m" + menuActive).addClass("active");

    if ($("#m" + menuActive).attr("mst") !== undefined) $("#" + $("#m" + menuActive).attr("mst")).addClass("active");

    $(".page-link a").live("click", function () {
        var elRender = $(this).parent().parent().attr("rdr");
        page = $(this).attr("page");
        $.ajax({
            url: $(this).attr("href") + "&sort=" + sort + "&direction=" + direction,
            type: "POST",
            data: param,
            success: function (msg) {
                if (msg) {
                    $("#" + elRender).html(msg);
                }
            }
        });

        $.ajax({
            url: $(this).attr("href").replace("AjaxTable", "AjaxPaging") + "&sort=" + sort + "&direction=" + direction,
            type: "POST",
            data: param,
            success: function (msg) {
                if (msg) {
                    $("[rdr='" + elRender + "']").html(msg);
                }
            }
        });
        return false;
    });

    $(".eq-sort").live("click", function () {
        var elRender = $(this).attr("des");
        sort = $(this).attr("sort");
        direction = $(this).attr("direction");

        if (direction.toLowerCase() == "asc") $(this).attr("direction", "desc");
        else $(this).attr("direction", "asc");

        $.ajax({
            url: URL + "?page=" + page + "&sort=" + sort + "&direction=" + direction,
            type: "POST",
            data: param,
            success: function (msg) {
                if (msg) {
                    $("#" + elRender).html(msg);
                }
            }
        });

        $.ajax({
            url: URL.replace("AjaxTable", "AjaxPaging") + "?page=" + page + "&sort=" + sort + "&direction=" + direction,
            type: "POST",
            data: param,
            success: function (msg) {
                if (msg) {
                    $("[rdr='" + elRender + "']").html(msg);
                }
            }
        });
    });

    $(".eq-search").live("keyup", function (e) {
        var elRender = $(this).parent().parent().attr("des");
        if (e.keyCode == 13) {
            var field = "";
            var value = "";
            page = 1;
            param = [];
            $(".eq-search").each(function () {
                field = $(this).attr("field");
                value = $(this).val();

                if (value != "") param.push({ column: field, value: value });
            });

            $.ajax({
                url: URL + "?page=" + page + "&sort=" + sort + "&direction=" + direction,
                type: "POST",
                data: { filter: JSON.stringify(param) },
                success: function (msg) {
                    if (msg) {
                        $("#" + elRender).html(msg);
                    }
                }
            });

            $.ajax({
                url: URL.replace("AjaxTable", "AjaxPaging") + "?page=" + page + "&sort=" + sort + "&direction=" + direction,
                type: "POST",
                data: { filter: JSON.stringify(param) },
                success: function (msg) {
                    if (msg) {
                        $("[rdr='" + elRender + "']").html(msg);
                    }
                }
            });
        }
    });

    $('.account-trigger').click(function () {
        $.ajax({
            url: "/Account/Trigger",
            type: "GET",
            success: function (msg) {
                if (msg) {
                    $.blockUI({
                        message: msg,
                        centerX: true,
                        css: {
                            width: '800px',
                            border: 'none',
                            padding: '5px',
                            backgroundColor: '#FFF',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            top:  ($(window).height() - 500) /2 + 'px', 
                            left: ($(window).width() - 800) /2 + 'px'
                        }
                    });

                    $('.blockOverlay').click($.unblockUI);
                }
            }
        });
    });
});